package com.kanq.clockin.schedule;

import com.kanq.clockin.entity.User;
import com.kanq.clockin.service.IClockService;
import com.kanq.clockin.utils.EmailUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Random;


@Configuration
@EnableScheduling
@Slf4j
public class ClockInScheduleTask {

    @Autowired
    IClockService clockService;

    /**
     *  每个工作日 周一 至 周五  每天  8点 12点 13点 18点 31分 自动执行定时任务
     */
    @Scheduled(cron = "0 31 8,13,18 * * 1,2,3,4,5")
    private void task() throws Exception {
        User[] values = User.values();
        Random random = new Random();
        int i = random.nextInt(600000);
        Thread.sleep(i);
        for (User user: values) {
            String res = clockService.actionClock(user);
            log.info("给用户==:{}==邮箱==:{}==发送邮件====:{}", user.getName(), user.getEmail(), res);
            EmailUtil.sendEmail(user.getEmail(), res);
        }
    }

    @Scheduled(cron = "0 01 12 * * 1,2,3,4,5")
    private void task2() throws Exception {
        User[] values = User.values();
        Random random = new Random();
        int i = random.nextInt(300000);
        Thread.sleep(i);
        for (User user: values) {
            String res = clockService.actionClock(user);
            log.info("给用户==:{}==邮箱==:{}==发送邮件====:{}", user.getName(), user.getEmail(), res);
            EmailUtil.sendEmail(user.getEmail(), res);
        }
    }
}
